package com.lz.football.module.leagueMatch.view
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.liangzhong.events.LeagueMatchEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.club.vo.NpcClubInfoVo;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.leagueMatch.item.ChampionItem;
	import com.lz.football.module.leagueMatch.item.TeamIconItem;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.ImageContain;

	/**
	 *冠军赛 
	 * @author lake
	 * 
	 */	
	public class ChampionMatchPanel extends Sprite implements ILeagueMatchPanel
	{
		/**
		 * 皮肤
		 */		
		public var _symbolSkin			:MovieClip;
		/**
		 *条目列表 
		 */		
		private var itemList			:Array = [];
		/**
		 * TextField 
		 */		
		private var scoreList			:Array = [];
		/**
		 *挑战 
		 */		
		private var challengeBtn		:SimpleButton;
		/**
		 *队徽容器 
		 */		
		private var _image 			:ImageContain;
		
		public function ChampionMatchPanel()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */	
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.leaguematch.view.ChampionMatchPanel");
			this.addChild(_symbolSkin);
			challengeBtn = _symbolSkin.challengeBtn;
			/*var i:int,j :int;
			var item :TeamIconItem;
			var txtScore :TextField;
			for(i = 1;i <= 15;i++)
			{
				for(j=0;j<2;j++)
				{
					item = new TeamIconItem();
					item.initUI(_symbolSkin["item_"+i+"_"+j]);
					itemList.push(item);
					//txt
					txtScore = _symbolSkin["txtScore_"+i+"_"+j];
					txtScore.text = "";
					scoreList.push(txtScore);
				}
			}*/
			var i :int = 0;
			var item :ChampionItem;
			for(;i<5;i++)
			{
				item = new ChampionItem();
				item.initUI(_symbolSkin["item_"+i]);
				item.index = i;
				itemList.push(item);
			}
			_symbolSkin.txtName1.text = "" + AppData.getInstance().clubDatamanager.clubInfo.uname;
			addEvtListener();
			//自己队徽
			var iconId :int = AppData.getInstance().clubDatamanager.clubInfo.logo;
			var iconDisp :DisplayObject = Reflection.createInstance("com.football.team.icon_"+iconId);
			if(iconDisp == null)return;
			var showSize :Number = 125;
			var scxy :Number = ((showSize / iconDisp.width) <= (showSize / iconDisp.height))?(showSize / iconDisp.width):(showSize / iconDisp.height);
			iconDisp.scaleX = scxy;
			iconDisp.scaleY = scxy;
			DisplayObjectUtil.removeAllChildren(_symbolSkin.container1);
			_symbolSkin.container1.addChild(iconDisp);
			iconDisp.x = (showSize - iconDisp.width)/2;
			iconDisp.y = (showSize - iconDisp.height)/2;
		}
		/**
		 *注册事件 
		 * 
		 */	
		public function addEvtListener():void
		{
			AppData.getInstance().leagueMatchManager.championMatchInfoVo.addEventListener(Event.CHANGE,dataChangeHandler);
			challengeBtn.addEventListener(MouseEvent.CLICK,challengeHandler);
		}
		/**
		 *挑战 
		 * @param e
		 */		
		private function challengeHandler(e :MouseEvent):void
		{
			var param :Object = {};
			param.cid = 0;
			this.dispatchEvent(new LeagueMatchEvent(LeagueMatchEvent.CHAMPION_MATCH_START,param,true));
			//disable
			challengeBtn.mouseEnabled = false;
			challengeBtn.filters = [DisplayObjectUtil.DISABLE_FILTER];
		}
		/**
		 *数据改变 
		 * @param e
		 */		
		private function dataChangeHandler(e :Event = null):void
		{
			/*var nums :Array = [0,8,12,14,15];
			var matchResList :Array = AppData.getInstance().leagueMatchManager.championMatchInfoVo.matchResList;
			var matchIndex :int = AppData.getInstance().leagueMatchManager.championMatchInfoVo.matchindex;
			var toNum :int = nums[matchIndex];
			var txtIndex :int = nums[matchIndex - 1];
			var i:int,j :int;
			var item :TeamIconItem;
			var txtScore :TextField;
			var rindex :int = 0;
			var obj :Object;
			for(i = 0;i < toNum;i++)
			{
				for(j=0;j<2;j++)
				{
					obj = matchResList[i];
					item = itemList[rindex];
					txtScore = scoreList[rindex];
					if(i < txtIndex)
					{
						if(j == 0)
						{
							txtScore.text = obj.hgoal + "";
						}
						else
						{
							txtScore.text = obj.ggoal + "";
						}
					}
					item.data = obj;
					rindex ++ ;
				}
			}*/
			var matchResList :Array = AppData.getInstance().leagueMatchManager.championMatchInfoVo.matchResList;
			var matchIndex :int = AppData.getInstance().leagueMatchManager.championMatchInfoVo.matchindex;
			matchIndex = matchIndex - 1;
			var i :int = 0;
			var item :ChampionItem;
			var obj :Object;
			for(;i<5;i++)
			{
				item = itemList[i];
				obj = matchResList[i];
				if(i < matchIndex)
				{
					obj.status = 1;//已经打过
				}
				else if(i == matchIndex)
				{
					obj.status = 2;//正在打
					_symbolSkin.flagMc.gotoAndStop((i+1));
					var npcClubVo :NpcClubInfoVo = AppData.getInstance().npcClubManager.getNpcClubById(obj.gcid);
					if(npcClubVo)
					{
						_symbolSkin.txtName2.text = "" + npcClubVo.name;
						if(_image == null)
						{
							_image = new ImageContain();
							_image.setSize(125,125);
						}
						_image.url = npcClubVo.url_125;
						DisplayObjectUtil.removeAllChildren(_symbolSkin.container2);
						_symbolSkin.container2.addChild(_image);
					}
				}
				else
				{
					obj.status = 3;//未打过
				}
				item.data = obj;
			}
		}
		/**
		 *删除事件 
		 * 
		 */	
		public function removeEvtListener():void
		{
			AppData.getInstance().leagueMatchManager.championMatchInfoVo.removeEventListener(Event.CHANGE,dataChangeHandler);
			challengeBtn.removeEventListener(MouseEvent.CLICK,challengeHandler)
		}
		/**
		 *销毁 
		 * 
		 */	
		public function destroyView():void
		{
			removeEvtListener();
			/*for each(var item :TeamIconItem in itemList)
			{
				item.destroyView();
				item = null;
			}*/
			for each(var item :ChampionItem in itemList)
			{
				item.destroyView();
				item = null;
			}
			if(_image)
			{
				_image.clear();
				_image = null;
			}
			itemList = null;
			_symbolSkin = null;
			challengeBtn = null;
			scoreList = null;
			DisplayObjectUtil.removeAllChildren(this);
		}
		/********************** implements interface *********************************/
		/**
		 * 比赛开始
		 */		
		public function matchStart(value :Object = null):void
		{
			
		}
		/**
		 * 比赛结束
		 */		
		public function matchEnd(value :Object = null):void
		{
			challengeBtn.mouseEnabled = true;
			challengeBtn.filters = null;
		}
		/**
		 * 错误返回
		 */		
		public function errorRes(value :Object = null):void
		{
			challengeBtn.mouseEnabled = true;
			challengeBtn.filters = null;
		}
	}
}